﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Yule8xCommon.Applib
{
    public class FormValidator
    {
        private HttpRequest request;
        public IList<string> Errors { get; set; }
        public FormValidator()
        {
            request = System.Web.HttpContext.Current.Request;
            Errors = new List<string>();
        }
        public bool Validate()
        {
            return Errors.Count < 1;
        }
        public bool CheckLengthByName(string name, string message,int maxLen, int minLen = 1)
        {
            string value = GetVarValue(name);
            return CheckLengthByValue(value, message, maxLen, minLen);
        }
        public bool CheckLengthByValue(string value, string message, int maxLen, int minLen = 1)
        {
            bool result = value.Length >= minLen && value.Length <= maxLen;
            if (!result)
            {
                Errors.Add(message);
            }
            return result;
        }
        public bool CheckRuleByName(string name, string rule, string message)
        {
            string value = GetVarValue(name);
            return CheckRuleByValue(value, rule, message);
        }
        /// <summary>
        /// 添加验证规则
        /// </summary>
        /// <param name="name"></param>
        /// <param name="rule"></param>
        /// <param name="message"></param>
        public bool CheckRuleByValue(string value,string rule,string message)
        {
            bool result = true;
            switch (rule)
            {
                case "require": // 必填
                    if (string.IsNullOrEmpty(value))
                    {
                        Errors.Add(message);
                        result = false;
                    }
                    break;
                case "number": // 数值
                    if (!string.IsNullOrWhiteSpace(value) && !RegularUtil.IsMatch(value, RegularUtil.Rules.Number))
                    {
                        Errors.Add(message);
                        result = false;
                    }
                    break;
                case "email": // 邮件地址
                    if (!string.IsNullOrWhiteSpace(value) && !RegularUtil.IsMatch(value, RegularUtil.Rules.Email))
                    {
                        Errors.Add(message);
                        result = false;
                    }
                    break;
                case "mobile": // 手机号码
                    if (!string.IsNullOrWhiteSpace(value) && !RegularUtil.IsMatch(value, RegularUtil.Rules.MobileNumber))
                    {
                        Errors.Add(message);
                        result = false;
                    }
                    break;
                case "username": // 用户名
                    if (!string.IsNullOrWhiteSpace(value) && !RegularUtil.IsMatch(value, RegularUtil.Rules.UserName))
                    {
                        Errors.Add(message);
                        result = false;
                    }
                    break;
                case "password": // 密码
                    if (!string.IsNullOrWhiteSpace(value) && !RegularUtil.IsMatch(value, RegularUtil.Rules.Password))
                    {
                        Errors.Add(message);
                        result = false;
                    }
                    break;
                case "url": // 网址
                    if (!string.IsNullOrWhiteSpace(value) && !RegularUtil.IsMatch(value, RegularUtil.Rules.URL))
                    {
                        Errors.Add(message);
                        result = false;
                    }
                    break;
                case "ip": // IP地址
                    if (!string.IsNullOrWhiteSpace(value) || !RegularUtil.IsMatch(value, RegularUtil.Rules.IP))
                    {
                        Errors.Add(message);
                        result = false;
                    }
                    break;
                default:
                    Errors.Add("验证规则无效!");
                    result = false;
                    break;
            }
            return result;
        }
        
        private string GetVarValue(string name)
        {
            string value;
            if (name.IndexOf('.') < 0)
            {
                value = request.Form[name];
                if (null == value)
                {
                    value = request.QueryString[name];
                }
                return value;
            }
            // post.name, get.name
            string[] names = name.Split('.');
            if("get"== names[0].ToLower())
            {
                value = request.QueryString[name];
            }else
            {
                value = request.Form[name];
            }
            return value;
        }
    }
}